package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Agent;
import tech.aistar.entity.House;
import tech.aistar.entity.Landlord;
import tech.aistar.entity.Message;
import tech.aistar.service.IAgentService;
import tech.aistar.service.IHouseService;
import tech.aistar.service.ILandlordService;
import tech.aistar.service.IMessageService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/AgentHouse")
public class AgentHouseController {
    @Autowired
    private IAgentService agentService;
    @Autowired
    private IHouseService houseService;
    @Autowired
    private ILandlordService landlordService;
    @Autowired
    private IMessageService messageService;



    @GetMapping("/house/{agentId}")
    public String select(HttpServletRequest request,@PathVariable("agentId") Integer agentId) {
//        String agentId = request.getParameter("agentId");
        Agent agent = agentService.selectById(Integer.valueOf(agentId));
        request.setAttribute("agent", agent);
        List<House> houseList = houseService.selectByagentId(Integer.valueOf(agentId));
        request.setAttribute("houseList",houseList);
        List<Landlord> landlordList = new ArrayList<>();
        for (int i = 0; i <houseList.size() ; i++) {
            Landlord landlord = landlordService.selectById(houseList.get(i).getLandlordId());
            landlordList.add(landlord);
        }
        request.setAttribute("landlordList",landlordList);
        List<Message> messageStates = messageService.selectByState(agent.getRegisterId());
        request.setAttribute("messageSize",messageStates.size());
        return "/personal_homepage/broker/agency";
    }
}
